package pdn.sci.cs.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import pdn.sci.cs.entity.ChildSponsor;
import pdn.sci.cs.entity.SystemUser;
import pdn.sci.cs.service.ChildSponsorService;
import pdn.sci.cs.service.SystemUserService;

@Scope(value = "prototype")
public class ChildSponsorAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	@Autowired private ChildSponsorService childSponsorService;
	@Autowired private SystemUserService systemUserService;

	private String childId;
	private ChildSponsor childSponsor;
	private List<ChildSponsor> list;
	private List<SystemUser> sponsorList;
	private List<String> sponsorIdStringList;

	public String list() {
		search();
		return SUCCESS;
	}

	public String searchForm() {
		searchPopulate();
		return SUCCESS;
	}

	public String frame() {
		return SUCCESS;
	}

	public String add() {
		addMode();
		addPopulate();
		return SUCCESS;
	}

	public String search() {
		if(childId != null) {
			list = childSponsorService.findByChildId(childId);
		} else {
			addActionError("Please give a criteria");
		}

		return SUCCESS;
	}

	public String edit() {
		editMode();
		return view();
	}

	public String save() {

		if(childSponsor != null) {
			//validateChild();
			if(hasErrors()) {
				add();
				return INPUT;
			} else {
				if(operationMode == OPERATION_MODE.ADD && childSponsor.getId().isEmpty()) {
					setAddSettings(childSponsor);

					ChildSponsor spn;
					for(String s: sponsorIdStringList) {
						spn = new ChildSponsor(new SystemUser(s), childSponsor.getChild());
						childSponsorService.save(spn);
					}
				} else if (operationMode == OPERATION_MODE.EDIT && !childSponsor.getId().isEmpty() ) {
					setUpdateSettings(childSponsor);
					childSponsorService.update(childSponsor);
				} else {
					addActionError("Error");
					add();
					return INPUT;
				}

			}
		} else {
			addActionError("Invalid Access");
			add();
			return INPUT;
		}
		childId = childSponsor.getChild().getId();
		return list();
	}

	public String view() {

		if(id == null || id.isEmpty()) {
			addActionError("Invalid Access");
			return INPUT;
		} else {
			childSponsor = childSponsorService.findById(id);
			childId = childSponsor.getChild().getId();
			if(childSponsor == null) {
				addActionError("Item that your are searching could not be found");
			}
		}
		return SUCCESS;
	}

	public String delete() {
		if(this.id.isEmpty()) {
			addActionError("Could not delete the entry, id is missing");
			return INPUT;
		} else {
			childSponsor = childSponsorService.findById(id);
			if(childSponsor != null) {
				childId = childSponsor.getChild().getId();
			}
			childSponsorService.delete(childSponsor);
			return list();
		}

	}

	private void searchPopulate() {

	}

	private void addPopulate() {
		sponsorList = systemUserService.findByUserRole(SystemUser.USER_ROLE.SPONSOR.name());
	}

	public String getChildId() {
		return childId;
	}

	public void setChildId(String childId) {
		this.childId = childId;
	}

	public ChildSponsor getChildSponsor() {
		return childSponsor;
	}

	public void setChildSponsor(ChildSponsor childSponsor) {
		this.childSponsor = childSponsor;
	}

	public List<ChildSponsor> getList() {
		return list;
	}

	public void setList(List<ChildSponsor> list) {
		this.list = list;
	}

	public List<SystemUser> getSponsorList() {
		return sponsorList;
	}

	public void setSponsorList(List<SystemUser> sponsorList) {
		this.sponsorList = sponsorList;
	}

	public List<String> getSponsorIdStringList() {
		return sponsorIdStringList;
	}

	public void setSponsorIdStringList(List<String> sponsorIdStringList) {
		this.sponsorIdStringList = sponsorIdStringList;
	}

}
